package com.example;

import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@WebServlet("/fastJsonServlet02")
public class FastJsonServlet02 extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        String username = request.getParameter("username");
        String age = request.getParameter("age");
        System.out.println(username+"---"+age);

        ArrayList<User> list=new ArrayList<>();
        //将数据批量添加到集合中
        Collections.addAll(list,new User(1,"ss",20),new User(2,"qq",18),new User(3,"tt",22));
        //JSON.toJSONString()将集合转换为字符串
        String str=JSON.toJSONString(list);

        //响应数据
        response.getWriter().print(str);
    }
}